package com.geektime.congestion.wallet.dao;

import com.geektime.congestion.wallet.domain.VirtualWallet;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class VirtualWalletDao {

    private static List<VirtualWallet> virtualWalletList = new ArrayList<>();

    public List<VirtualWallet> findAll() {
        return virtualWalletList;
    }

    public VirtualWallet save(VirtualWallet wallet) {
        List<VirtualWallet> list = virtualWalletList.stream()
                .filter(item -> item.getUuid().equals(wallet.getUuid())).collect(Collectors.toList());
        if (list.isEmpty()) {
            virtualWalletList.add(wallet);
        } else {
            virtualWalletList.remove(list.get(0));
            virtualWalletList.add(wallet);
        }
        return wallet;
    }

    public VirtualWallet findByUuid(String uuid) {
        List<VirtualWallet> list = virtualWalletList.stream()
                .filter(item -> item.getUuid().equals(uuid)).collect(Collectors.toList());
        return list.isEmpty() ? null : list.get(0);
    }

}
